package com.shangguan.atcrowdfunding.dispatchercontroller;

import com.shangguan.atcrowdfunding.bean.User;
import com.shangguan.atcrowdfunding.manager.service.UserService;
import com.shangguan.atcrowdfunding.utils.AjaxResult;
import com.shangguan.atcrowdfunding.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: DispatcherController
 * @Description:
 * @Author: 一尘
 * @Date: 2020 年 01 月 14 11:43
 * @Version 1.0
 */
@Controller
public class DispatcherController {

    @Autowired
    UserService userService;


    //跳转到我们的首页
    @RequestMapping("/main")
    public String main() {
        return "main";
    }

    //跳转到我们的首页
    @RequestMapping("/index")
    public String index() {
        return "index";
    }


    //跳转到我们的登录页面
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    // 登录的异步请求
    // 登录成功的用户名放置到session 中
    @RequestMapping("/doLogin")
    @ResponseBody
    public Object doLogin(String loginacct, String userpswd, String type, HttpSession session) {

        //  异步请求返回的结果值
        AjaxResult result = new AjaxResult();

        Map<String, Object> paramMap = new HashMap();
        paramMap.put("loginacct", loginacct);

        //密码进行MD5加密处理
        paramMap.put("userpswd", MD5Util.digest(userpswd));
        paramMap.put("type", type);

        // 和数据库的数据进行对比
        User user = userService.queryUserLogin(paramMap);
        System.out.println(user);
        if (user != null) {
            session.setAttribute("loginacct", loginacct);
            result.setSuccess(true);
            //重定向
            // return "redirect:main.htm";

        } else {
            result.setSuccess(false);
            result.setMessage("用户名或者密码不正确,请重新输入");
        }
        return result;
    }


    //同步的登录
   /* @RequestMapping("/doLogin")
    public String doLogin(String loginacct,String userpswd,String type){

        Map<String,Object> paramMap = new HashMap();
            paramMap.put("loginacct",loginacct);
            paramMap.put("userpswd",userpswd);
            paramMap.put("type",type);

        // 和数据库的数据进行对比
        User user =    userService.queryUserLogin(paramMap);
        System.out.println(user);
        if(user != null){
            return "main";
        }
        return "login";
    }*/


    /*退出系统*/

  /*  @RequestMapping("/loginout")
    public String loginout(HttpSession session){
        //销毁session
            session.invalidate();
        return "index";
    }*/


    @RequestMapping("/loginout")
    public String loginout(SessionStatus status) {
        //销毁session
        status.setComplete();
        return "index";
    }
}
